/**
 * bm.cp10.common
 * cp10
 */
package cn.ac.iscas.chimaera.common;

/**
 * @author yang yu
 *
 * @mail  yangyu@iscas.as.cn
 *
 * @time 2010-2-28下午09:03:08
 * 
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;



import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import org.apache.commons.lang.time.FastDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;



public class CommonAction extends ActionSupport {
	private static final long serialVersionUID = 572146812454l;
	private static final int BUFFER_SIZE = 16 * 1024;
	private static final Log log = LogFactory.getLog(CommonAction.class);

	private File myFile;
	private String contentType;
	private String fileName;
	private String imageFileName;
	private String caption;

	private String errormessage;
	private String redirectpage;
	

	private String path;
	private String returnsign;



	public CommonAction() {

	}

	

	private static void copy(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src),
						BUFFER_SIZE);
				out = new BufferedOutputStream(new FileOutputStream(dst),
						BUFFER_SIZE);
				byte[] buffer = new byte[BUFFER_SIZE];
				
				long file_size = src.length();
				long copy_times = file_size/BUFFER_SIZE;
				int remain_size = (int)file_size%BUFFER_SIZE;
				while ( copy_times > 0) {
					in.read(buffer);
					out.write(buffer);
					copy_times -- ;
				}
				
				//最后一次循环，有多余数据，单独处理
				if(remain_size!=0){
					buffer = new byte[remain_size];
					in.read(buffer);
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");
		return fileName.substring(pos);
	}


	public String removeFile() {

		log.debug("删除文件=======" + this.fileName);


		File delFile = new File(ServletActionContext.getServletContext()
				.getRealPath(""));

		if (delFile.delete())
			log.debug("删除文件成功=======" + delFile.getAbsolutePath());


		return SUCCESS;
	}

	public String getList() {

		log.debug("======= get filelist ===========");
		ActionContext context = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);


		File downloadFile = new File(ServletActionContext.getServletContext()
				.getRealPath("upload"));

        File[] downloadFiles = downloadFile.listFiles();
        ArrayList temp = new ArrayList();
        
        for (int i = 0; i < downloadFiles.length; i++) {

                temp.add(downloadFiles[i].getName());

        }
        request.setAttribute("filelist", temp);

		return SUCCESS;
	}
	
	@Override
	public String execute() {

		
		try{
			this.imageFileName = new Date().getTime() + getExtention(fileName);
			File imageFile = new File(ServletActionContext.getServletContext()
					.getRealPath("/upload")
					+ "/" + fileName);

			log.debug(imageFile.getAbsolutePath());

			copy(myFile, imageFile);



	
		}catch(Exception e){
			this.errormessage = "上传文件失败";
			this.redirectpage = "/chimaera/index.jsp";
			return Action.ERROR;
		}
		
		this.errormessage = "上传文件成功";
		this.redirectpage = "/chimaera/index.jsp";
		return Action.ERROR;
		
	}
	
	

	public void setMyFileContentType(String contentType) {
		this.contentType = contentType;
	}

	public void setMyFileFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public String getCaption() {
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}

	

	/**
	 * @return the path
	 */
	public String getPath() {
		return path;
	}

	/**
	 * @param path the path to set
	 */
	public void setPath(String path) {
		this.path = path;
	}
	
	public String getErrormessage() {
		return errormessage;
	}

	public void setErrormessage(String errormessage) {
		this.errormessage = errormessage;
	}

	public String getRedirectpage() {
		return redirectpage;
	}

	public void setRedirectpage(String redirectpage) {
		this.redirectpage = redirectpage;
	}



	public String getReturnsign() {
		return returnsign;
	}



	public void setReturnsign(String returnsign) {
		this.returnsign = returnsign;
	}

}